#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/21 11:33
# @Author  : 王凯
# @File    : project_clean.py
# @Project : spider-man
import datetime
import re
import warnings

import pandas as pd

from apps.tender_project.tender_project.clean import BaseCleanModel

warnings.filterwarnings("ignore")


def project_name(data):
    if data == "":
        return None
    if data is None:
        return None
    data = re.sub(
        r"【招标公告】|【竞争性磋商公告】|【竞争性谈判公告】|【公开招标】|【分散采购公告】|【采购公告】|【竞争性磋商】|【竞争性谈判】|竞争性磋商|竞争性谈判|采购公告|公开招标",
        "", data)
    biaodian = r'[=,.?!@#$%^&*_+:"<>/\[\]\\`~——，。、《》？；’：“”【】、{}|·！￥…（）-]'

    biaodian_list = re.findall(biaodian, data)

    for i in biaodian_list:
        i = "\\" + i

        data = re.sub(i, '', data)

    return data


def register_num(data):
    if data is None:
        return None
    biaodian = r'[=,.?!@#$%^&*()_+:"<>/\[\]\\`~——，。、《》？；’：“【】、{}|·！￥…（）-]'
    biaodian_list = re.findall(biaodian, data)
    for i in biaodian_list:
        i = "\\" + i
        data = re.sub(i, '', data)
    zhongwen = '[\u4e00-\u9fa5]'
    zhongwen_list = re.findall(zhongwen, data)
    for i in zhongwen_list:
        data = re.sub(i, '', data)
    return data


def buying_name(data):
    if data is None:
        return None
    data = re.sub(
        r"招标单位名称[:：为是]?|招标人[:：为是]?|招标单位[:：为是]?|招标方[:：为是]?|采购人[:：为是]?|采购单位[:：为是]？|采购单位名称[:：为是]?|采购方[:：为是]?|采购方名称[:：为是]?",
        "", data)
    return data


def budget_amount(data):
    if data is None:
        return None
    data = data * 10000

    # arabic_num = transform(data)

    return data


def accept_combo(data):
    if data is None:
        return None
    if "不接受" in data:
        data = 0
    elif "接受" in data:
        data = 1
    else:
        data = None

    return data


def contact_name(data):
    if data is None:
        return None
    biaodian = r'[=,.?!@#$%^&*()_+:"<>/\[\]\\`~——，。、《》？；’：“【】、{}|·！￥…（）-]'
    biaodian_list = re.findall(biaodian, data)
    for i in biaodian_list:
        i = "\\" + i
        data = re.sub(i, '', data)
    shuzi_list = re.findall(r"[0-9]|-", data)
    for i in shuzi_list:
        data = re.sub(i, '', data)

    return data


def agency(data):
    if data is None:
        return None
    biaodian = r'[=,.?!@#$%^&*()_+:"<>/\[\]\\`~——，。、《》？；’：“【】、{}|·！￥…（）-]'
    biaodian_list = re.findall(biaodian, data)
    for i in biaodian_list:
        i = "\\" + i
        data = re.sub(i, '', data)
    return data


def agency_tel(data):
    if data is None:
        return None
    biaodian = r'[=,.?!@#$%^&*()_+:"<>/\[\]\\`~——，。、《》？；’：“【】、{}|·！￥…（）]'
    biaodian_list = re.findall(biaodian, data)
    for i in biaodian_list:
        i = "\\" + i

        data = re.sub(i, '', data)

    zhongwen = '[\u4e00-\u9fa5]'
    zhongwen_list = re.findall(zhongwen, data)
    for i in zhongwen_list:
        data = re.sub(i, '', data)

    return data


class TenderClean(BaseCleanModel):

    def process_field_test(self, df_pro: pd.DataFrame) -> pd.DataFrame:
        """
        具体的清洗逻辑
        """
        for index, row in df_pro.iterrows():
            df_pro["project_name"][index] = project_name(row["project_name"])
            df_pro["register_num"][index] = register_num(row["register_num"])
            df_pro["buying_name"][index] = buying_name(row["buying_name"])
            df_pro["budget_amount"][index] = budget_amount(row["budget_amount"])
            df_pro["accept_combo"][index] = accept_combo(row["accept_combo"])
            df_pro["contact_name"][index] = contact_name(row["contact_name"])
            df_pro["agency"][index] = agency(row["agency"])
            df_pro["agency_tel"][index] = agency_tel(row["agency_tel"])

        return df_pro


if __name__ == "__main__":
    test = TenderClean(
        origin_table="net_tender_info", target_table="net_tender_info_new", redis_task_keys="net_tender_info:wash:task"
    )
    test.time_wash(
        (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"),
        datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    )
